python

LC 58 Length of Last Word

```class Solution(Object): def lengthOfLastWord(self, s): return len(s.strip().split(' ')[-1])


[-1] 代表最后一个元素, split(' ') 代表按照空格拆分, strip() 方法用于移除字符串头尾指定的字符(默认为空格)。

LC 203 Remove the Linked List Elements

class ListNode(object): def init(self, x): self.val = x self.next = None

class Solution(object): def removeElements(self, head, val): if head == None: return head dummy = ListNode(0) dummy.next = head pre = dummy while head: if head.val == val: pre.next = head.next head = pre pre = head head = head.next return dummy.next ``` 判断 linkedlist 是否有下一个节点,直接 while head:

results matching ""

    No results matching ""